package com.tavenli.repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.tavenli.entity.DeviceCmdEntity;
import com.tavenli.entity.F18Device;
import com.tavenli.model.PageData;

@Repository
public class DeviceCmdDao extends BaseDao {
	private static Logger logger = LoggerFactory.getLogger(UserDao.class);
	
	public DeviceCmdEntity getDeviceCmdById(int id){
		try {
			return this.getById(DeviceCmdEntity.class, id);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return null;
		}
	}
	
	public int queryDataCount(Map<String, Object> paramMap){
		StringBuilder hql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();
		
		hql.append("select count(*) from DeviceCmdEntity where 1=1");
		
		if(paramMap.get("id")!=null){
			hql.append(" and id=:id");
			params.put("id", paramMap.get("id"));
		}
		if(paramMap.get("sn")!=null){
			hql.append(" and sn like:sn");
			params.put("sn", "%"+ paramMap.get("sn") +"%");
		}
		return super.queryPageTotalCount(hql.toString(),params).intValue();
	}
	
	public List<DeviceCmdEntity> queryPageData(int start, int maxSize,Map<String, Object> paramMap){
		StringBuilder hql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();
		
		hql.append("from DeviceCmdEntity  where 1=1");
		
		if(paramMap.get("id")!=null){
			hql.append(" and id=:id");
			params.put("id", paramMap.get("id"));
		}
		
		if(paramMap.get("sn")!=null){
			hql.append(" and sn like:sn");
			params.put("sn", "%"+ paramMap.get("sn") +"%");
		}
		
		hql.append(" order by id desc");
		
		return super.queryPageList(hql.toString(), params, start, maxSize);
	}

	public int deleteAll() {
		String hql = "delete DeviceCmdEntity";
		Query query = this.getEntityManager().createQuery(hql);
		try {
			return query.executeUpdate();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return -1;
	}
}
